package com.evan.uyclub;

import android.animation.ArgbEvaluator;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.evan.uyclub.About.About_me;
import com.evan.uyclub.HomeFragment.AppFragment;
import com.evan.uyclub.HomeFragment.FragmentAdapter;
import com.evan.uyclub.HomeFragment.HappyFragment;
import com.evan.uyclub.HomeFragment.HomeFragment;
import com.evan.uyclub.HomeFragment.LazyFragment;
import com.evan.uyclub.HomeFragment.MyFragment;
import com.evan.uyclub.HomeFragment.ToolFragment;
import com.evan.uyclub.LoadDiaLongs.DisActivity;
import com.evan.uyclub.Other.BrowserActivity;
import com.evan.uyclub.Other.SetClass;
import com.gauravk.bubblenavigation.BubbleNavigationConstraintView;
import com.gjiazhe.multichoicescirclebutton.MultiChoicesCircleButton;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.sdsmdg.tastytoast.TastyToast;
import com.wx.ovalimageview.RoundImageView;

import java.util.ArrayList;
import java.util.Objects;

public class StartActivity extends AppCompatActivity {


    ArrayList<LazyFragment> fragments = new ArrayList<>();
    private ConstraintLayout all_main_start;
    private MultiChoicesCircleButton multi; // 多项选择
    private Keyframe objectAnimator;
    private SharedPreferences data;
    private TextView ycgg;
    private View activity;
    private ViewPager2 view_pager;
    // 下载APK目录
    private String apkPath;
    // 下载进度
    private TextView mProgressText;
    private BubbleNavigationConstraintView bottom_navigation;
    private RoundImageView icon;
    private BottomSheetDialog pop;
    private String new_url, aixin;
    private DisActivity disActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        all_main_start = findViewById(R.id.all_main_start);
        //和逐渐显示动画
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(all_main_start, "alpha", 0f, 1f);
        objectAnimator.setDuration(1500);
        objectAnimator.start();
        SetClass.status(this); // 设置状态栏
        // 把xml界面的avtivity配给同一个变量，
        fragments.add(new HomeFragment());
        fragments.add(new AppFragment());
        fragments.add(new ToolFragment());
        fragments.add(new HappyFragment());
        fragments.add(new MyFragment());
        //开始弹窗
        initView(); // 初始化

    }

    private void initView() {
        view_pager = findViewById(R.id.view_pager);
        bottom_navigation = findViewById(R.id.bottom_navigation);
        multi = findViewById(R.id.multi);
        //下面是隐藏某个控件代码
        multi.setVisibility(View.VISIBLE); // 多项选择隐藏初始化
        bottom_navigation.setTypeface(Typeface.createFromAsset(getAssets(), "space.ttf")); // 设置字体
        //获取点击下面buttom——navigation的值，分别是0，1，2，3，4总共五个；
        bottom_navigation.setBadgeValue(0, null);
        bottom_navigation.setBadgeValue(1, null);
        bottom_navigation.setBadgeValue(2, null);
        bottom_navigation.setBadgeValue(3, null);
        bottom_navigation.setBadgeValue(4, null);
        //这里是开源库，传给开源库，进行处理；
        view_pager.setAdapter(new FragmentAdapter(this, fragments));
        view_pager.setOffscreenPageLimit(5); // 页面预加载
        view_pager.setUserInputEnabled(false); // 禁止手动滑动
        //这里是把xml界面显示在pagger上，也就是显示；
        view_pager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                bottom_navigation.setCurrentActiveItem(position);
            }
        });
        //这里是对下面的导航栏进行时间，点击切换界面及其他控件；
        bottom_navigation.setNavigationChangeListener((view, position) -> { // 界面切换事件
            view_pager.setCurrentItem(position, false); // 点击切换界面 禁止滚动
            if (position == 0) {
                if (multi.getVisibility() == View.VISIBLE) {
                    //  multiAnim(); // 多项选择消失
                    // 多项选择显示
                    Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.in_multi_down);  //这是让multi动画形式出现的anim调用
                    multi.startAnimation(shakeAnim); //进行动画（先赋值，再调用，这里是调用）；
                    multi.setVisibility(View.VISIBLE);  //显示
                    /**
                     *  VISIBLE  ----   显示
                     *  GONE     ----   隐藏
                     * **/
                    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                    view_pager.startAnimation(shake);
                    SetClass.status(this);
                    //设置动画颜色
                    White();
                    //播放音频
                    Music();
                }
            } else if (position == 1) {
                if (multi.getVisibility() == View.VISIBLE) {
                    // 多项选择显示
                    Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.in_multi_down);
                    multi.startAnimation(shakeAnim);
                    multi.setVisibility(View.VISIBLE);
                    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                    view_pager.startAnimation(shake);
                    SetClass.status(this);
                    //设置动画颜色
                    White();
                    //播放音频
                    Music();
                }
            } else if (position == 2) {
                if (multi.getVisibility() == View.VISIBLE) {
                    // multiAnim(); // 多项选择消失
                    // 多项选择显示
                    Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.in_multi_down);
                    multi.startAnimation(shakeAnim);
                    multi.setVisibility(View.VISIBLE);
                    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                    view_pager.startAnimation(shake);
                    SetClass.status(this);
                    //设置动画颜色
                    White();
                    //播放音频
                    Music();
                }
            } else if (position == 3) {
                if (multi.getVisibility() == View.VISIBLE) {
                    // multiAnim(); // 多项选择消失
                    // 多项选择显示
                    Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.in_multi_down);
                    multi.startAnimation(shakeAnim);
                    multi.setVisibility(View.VISIBLE);
                    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                    view_pager.startAnimation(shake);
                    SetClass.statustop(this);
                    oNwhite();
                    //播放音频
                    Music();
                }
            } else if (position == 4) {
                if (multi.getVisibility() == View.VISIBLE) {
                    // multiAnim(); // 多项选择消失
                    // 多项选择显示
                    Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.in_multi_down);
                    multi.startAnimation(shakeAnim);
                    multi.setVisibility(View.VISIBLE);
                    Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
                    view_pager.startAnimation(shake);
                    SetClass.status(this);
                    //设置动画颜色
                    White();
                    //播放音频
                    Music();
                }
            }
        });
        // 添加多项选择内容
        ArrayList<MultiChoicesCircleButton.Item> buttonItems = new ArrayList<>();
        //  标题，背景图，位置  ----- 然后赋给相应变量
        MultiChoicesCircleButton.Item item1 = new MultiChoicesCircleButton.Item("我的作品", ContextCompat.getDrawable(StartActivity.this, R.mipmap.app_by), 20);
        buttonItems.add(item1);
        MultiChoicesCircleButton.Item item2 = new MultiChoicesCircleButton.Item("问题反馈", ContextCompat.getDrawable(StartActivity.this, R.mipmap.bug_app), 55);
        buttonItems.add(item2);
        MultiChoicesCircleButton.Item item3 = new MultiChoicesCircleButton.Item("官方群聊", ContextCompat.getDrawable(StartActivity.this, R.mipmap.kuanzeng), 90);
        buttonItems.add(item3);
        MultiChoicesCircleButton.Item item4 = new MultiChoicesCircleButton.Item("关于应用", ContextCompat.getDrawable(StartActivity.this, R.mipmap.guanyu), 125);
        buttonItems.add(item4);
        MultiChoicesCircleButton.Item item5 = new MultiChoicesCircleButton.Item("联系我们", ContextCompat.getDrawable(StartActivity.this, R.mipmap.lianxi), 160);
        buttonItems.add(item5);
        multi.setButtonItems(buttonItems);
        //这里就是item选择相应的事件哦
        multi.setOnSelectedItemListener((item, index) -> {
            // 判断应用操作类型
            if (Objects.equals(item.getText(), "我的作品")) {
                Intent intent3 = new Intent();
                intent3.putExtra("网址", "https://jfkj.xyz/");
                intent3.setClass(StartActivity.this, BrowserActivity.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
            } else if (Objects.equals(item.getText(), "问题反馈")) {
                Intent intent3 = new Intent();
                intent3.putExtra("网址", "http://support.qq.com/products/410589");
                intent3.setClass(StartActivity.this, BrowserActivity.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
            } else if (Objects.equals(item.getText(), "官方群聊")) {
                /*Intent intent3 = new Intent();
                intent3.setClass(StartActivity.this, About_me.class);
                startActivity(intent3);
                overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);*/
                joinQQGroup("4wHKnqMDHP0xdrk73XK0RaUs699QJHGH");
            } else if (Objects.equals(item.getText(), "关于应用")) {
                Intent intent = new Intent();
                intent.setClass(StartActivity.this, About_me.class);
                startActivity(intent);
                overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
            } else if (Objects.equals(item.getText(), "联系我们")) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + "3487297072" + "&version=1")));
                overridePendingTransition(R.anim.activity_visible, R.anim.activity_gone);
            } else {
                finish(); // 关闭软件
                Toast.makeText(StartActivity.this, "软件异常", Toast.LENGTH_SHORT).show();  //  无处理事件
            }

        });
    }

    //返回键    //返回键检测并弹窗
    public void onBackPressed() {
        disActivity = new DisActivity(StartActivity.this);
        disActivity.show(true);
    }

    protected void onDestroy() {
        if (disActivity != null) {
            disActivity.dismiss();
        }
        super.onDestroy();
    }


    //播放音乐
    public void Music() {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.a);
        mediaPlayer.start();
    }

    public void White() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(bottom_navigation, "backgroundColor", getColor(R.color.white), getColor(R.color.white));
        objectAnimator.setEvaluator(new ArgbEvaluator());//设置估值器
        objectAnimator.setDuration(1000);
        objectAnimator.start();
    }

    public void oNwhite() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(bottom_navigation, "backgroundColor", getColor(R.color.white), getColor(R.color.炫酷黑));
        objectAnimator.setEvaluator(new ArgbEvaluator());//设置估值器
        objectAnimator.setDuration(1000);
        objectAnimator.start();

        ObjectAnimator objectAnimators = ObjectAnimator.ofFloat(view_pager, "alpha", 0f, 1f);
        objectAnimators.setDuration(1000);
        objectAnimators.start();
    }

    public void HappyToast(View view) {
        TastyToast.makeText(this, "感谢您的支持,我会尽力维护!🔃", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS).show();
    }

    public void Toast(View view) {
        TastyToast.makeText(this, "努力开发中...🧑‍💻", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS).show();
    }

    public void newToast(View view) {
        TastyToast.makeText(this, "亲爱的,该版本为强更新哦:)🥹", TastyToast.LENGTH_SHORT, TastyToast.WARNING).show();
    }

    public void Search(View view) {
        TastyToast.makeText(this, "努力开发中...🧑‍💻", TastyToast.LENGTH_SHORT, TastyToast.ERROR).show();
    }

    /****************
     *
     * 发起添加群流程。群号：UYClub(1020165193) 的 key 为： T2M0EPIzJpp5NtefXbEN_zs9KQEIzQC8
     * 调用 joinQQGroup(T2M0EPIzJpp5NtefXbEN_zs9KQEIzQC8) 即可发起手Q客户端申请加群 UYClub(1020165193)
     *
     * @param key 由官网生成的key
     * @return 返回true表示呼起手Q成功，返回false表示呼起失败
     ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }
}